/* Copyright (C) 2021 Rain */

/* This file is part of XNIX. */

/* 
  XNIX is free software: you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation, either version 3 of the License, or 
  (at your option) and later version. 
*/

/*
  XNIX is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  GNU General Public License for more details. 
*/

/*
  You should have received a copy of the GNU General Public License 
   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
*/




#include <kernel/init.h>
#include <kernel/types.h>

#include <lib/fifo.h>


void fifo_init(struct fifo *fifo, __fifo_buf_t *buf, int total) {
	fifo->buf = buf;
	fifo->total = total;
	
	fifo->next_wr = fifo->next_rd = 0;
	fifo->need_read = 0;

	return;
}


__fifo_buf_t fifo_get(struct fifo *fifo) {
	__fifo_buf_t res;

	if (!fifo->need_read) 
		return 0;

	fifo->need_read--;

	res = fifo->buf[fifo->next_rd++];

	if (fifo->next_rd == fifo->total) 
		fifo->next_rd = 0;

	return res;
}


void fifo_put(struct fifo *fifo, __fifo_buf_t value) {
	fifo->buf[fifo->next_wr++] = value;
	fifo->need_read++;

	if (fifo->next_wr == fifo->total) 
		fifo->next_wr = 0;

	return;
}
